package com.hzhc.controller.sysmanage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.SysUser;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysUserRoleService;
import com.hzhc.service.SysUserService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.SysUserVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 功不唐捐
 * @since 2024-04-07
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class UserController extends BaseController {

    private final SysUserService userService;

    private final SysUserRoleService userRoleService;

    @Value("${file.userUploadFolder}")
    private String userUploadFolder;
    @Value("${file.userAccessPath}")
    private String userAccessPath;

    /**
     * 用户管理列表
     *
     * @param request
     * @param data
     * @return
     */
    @RequestMapping("/getDataGreduserlist")
    public ResultUtil<List<SysUserVo>> getDatagridUserList(HttpServletRequest request, @RequestBody SysUserVo data) {
        Page<SysUserVo> page = new Page<>(data.getPageIndex(), data.getPageSize());
        List<SysUserVo> userList = userService.findUserList(page, data);
        return ResultUtil.build(userList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 用户管理新增
     *
     * @param request
     * @param data
     * @return
     */
    @PostMapping("/saveUser")
    @SysTemLog(operModul = "用户管理-用户新增", operType = "新增", operDesc = "用户新增功能", modular = "PC")
    public ResultUtil<T> saveUser(HttpServletRequest request, SysUserVo data,
                                  @RequestParam(value = "fileRef", required = false) MultipartFile fileRef) {
        data.setFile(fileRef);
        return userService.saveUser(data, userUploadFolder, userAccessPath);
    }

    /**
     * 用户管理删除
     *
     * @return
     */
    @PostMapping("/deleteUser")
    @SysTemLog(operModul = "主页面-用户删除", operType = "删除", operDesc = "用户删除", modular = "PC")
    public ResultUtil<T> deleteUser(@RequestBody SysUserVo data) {
        return userService.updateDelete(data.getIds());
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPasswd")
    @SysTemLog(operModul = "主页面-用户重置密码", operType = "重置密码", operDesc = "用户重置密码功能", modular = "PC")
    public ResultUtil<T> resetPasswd(@RequestBody SysUser data) {
        Boolean b = userService.updateById(data);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    /**
     * 用户管理校验登入名
     *
     * @return
     */
    @RequestMapping("/checkUserLoginname")
    public ResultUtil<T> checkUserLoginname(@RequestBody SysUserVo data) {
        String field = "loginname";
        Boolean bool = userService.checkUserByField(field, data);
        if (bool) {
            return ResultUtil.build(ResultUtilCodeEnum.LOGIN_NAME_AlREAD_EXIST);
        } else {
            return ResultUtil.ok();
        }
    }

    /**
     * 用户管理校验手机号
     *
     * @return
     */
    @RequestMapping("/checkUserMobile")
    public ResultUtil<T> checkUserMobile(@RequestBody SysUserVo data) {
        String field = "mobile";
        Boolean bool = userService.checkUserByField(field, data);
        if (bool) {
            return ResultUtil.build(ResultUtilCodeEnum.MOBILE_AlREAD_EXIST);
        } else {
            return ResultUtil.ok();
        }
    }

    /**
     * 用户授权
     *
     * @return
     */
    @RequestMapping("/userRoleSave")
    @SysTemLog(operModul = "用户管理-用户授权", operType = "用户授权", operDesc = "用户授权功能", modular = "PC")
    public ResultUtil<T> userRoleSave(@RequestBody SysUserVo vo) {
        return userRoleService.saveUserRole(vo);

    }

    /**
     * 用户管理修改
     *
     * @return
     */
    @RequestMapping("/userUpdate")
    @SysTemLog(operModul = "用户管理-用户修改", operType = "修改", operDesc = "用户修改功能", modular = "PC")
    public ResultUtil<T> userUpdate(SysUserVo data,
                                    @RequestParam(value = "fileRef", required = false) MultipartFile fileRef) {
        data.setFile(fileRef);
        return userService.updateUser(data, userUploadFolder, userAccessPath);
    }

    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping("/changePasswordRet")
    @SysTemLog(operModul = "主页面-用户修改密码", operType = "修改密码", operDesc = "用户修改密码功能", modular = "PC")
    public ResultUtil<T> changePasswordRet(HttpServletRequest request, @RequestBody SysUserVo data) {
        UserDto user = TokenUtil.getUser(request);
        data.setId(user.getId());
        return userService.changeUserPassword(data);
    }

    /**
     * 角色授权回显
     */
    @RequestMapping("/getUserList")
    public ResultUtil<List<Long>> getUserList(@RequestBody SysUserVo vo) {
        List<Long> userList = userService.getUserList(vo.getId());
        return ResultUtil.build(userList, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 解锁
     * @param vo
     * @return
     */
    @PostMapping("/unlockRet")
    @SysTemLog(operModul = "用户管理-用户解锁", operType = "解锁", operDesc = "用户解锁功能", modular = "PC")
    public ResultUtil<T> unlockRet(@RequestBody SysUserVo vo) {
        boolean bool = userService.unlockRet(vo);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

}
